﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoolCode.DataSource.DB4O
{
    public  class DataProvider:DataBase,IDataProvider 
    {
        public DataProvider()
            : base()
        {
        }

        public DataProvider(string connectionString) : base(connectionString) { }

        public IList<T> Query<T>(Guid guid) where T : IEntity
        {
            return Query<T>(c => c.Guid == guid);
        }

        public bool Contains<T>(T TEntity) where T : IEntity
        {
            var q = Query<T>(TEntity.Guid);
            return q.Count > 0;
        }

        public void Update<T>(T TEntity) where T : IEntity
        {
            if (Contains<T>(TEntity))
            {
                var q = Query<T>(TEntity.Guid);
                DeleteAll(q);
            }
            Store(TEntity);
        }
    }
}
